﻿// Based on Jquery
$.prototype.comboxBindingIn = function (data, displayField, valueField) {
    var selectControl = $("<select/>");
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    this.append(selectControl);
    return this;
};
$.prototype.comboxBinding = function (data, displayField, valueField) {
    var selectControl = this.empty();
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    return this;
};
$.prototype.comboxBindingWithEmptyDefault = function (data, displayField, valueField) {
    var selectControl = this.empty();
    $('<option/>').appendTo(selectControl);
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    return this;
};
Date.prototype.toChineseDateFormat = function () {
    if (arguments.length == 0)
        return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate();
    switch (arguments[0]) {
        case "yyyy-MM":
            return this.getFullYear() + "-" + (this.getMonth() + 1);
    }
};
String.prototype.toDateTime = function () {
    if (this.indexOf(".") > 0) {
        var data = this.split('.');
        return new Date(data[0], data[1] - 1, data[2]); 
    }
    else if (this.indexOf("-") > 0) {
        var data = this.split('-');
        return new Date(data[0], data[1] - 1, data[2]); 
     }
    else if (this.indexOf("/") > 0) {
        var data = this.split('/');
        return new Date(data[2], data[0] - 1, data[1]); 
    }
}
Date.prototype.toEpoch = function () {
    if (!this) return null;
    return Math.round(this.getTime() / 1000);
};
Date.fromEpoch = function (epoch) {
    if (!epoch) return null;
    return new Date(epoch * 1000);
};
Date.prototype.getDifferDayCount = function (targetDate) {
    return Math.abs((this - targetDate) / 86400000);
};
Date.prototype.addDays = function (n) {
    this.setDate(this.getDate() + 0 + n);
    return this;
}
Date.prototype.addMonths = function (n) {
    this.setMonth(this.getMonth() + 0 + n);
    return this;
}


// 针对IE下 resize 事件假死的fix
$.fn.wresize = function (f) {
    version = '1.1';
    wresize = { fired: false, _lastWidth: 0, _lastHeight: 0 };
    timeout = 50;

    function handleWResize(e) {
        if ($.browser.msie) {
            if (!wresize.fired) {
                setTimeout(function () {
                    wresize.fired = false;
                }, timeout);

                // 触发resize后，只有widht和height真正发生改变才触发事件
                if (wresize._lastWidth != $(window).width() || wresize._lastHeight != $(window).height()) {
                    wresize.fired = true;
                    wresize._lastWidth = $(window).width();
                    wresize._lastHeight = $(window).height();

                    return f.apply(this, [e]);
                }

            }
        } else {
            // 非IE浏览器直接触发事件
            return f.apply(this, [e]);
        }
        return false;
    }

    this.each(function () {
        if (this == window) {

            $(this).resize(handleWResize);
        }
        else {
            $(this).resize(f);
        }
    });

    return this;
};